package com.zang.blogz.controller;


import com.zang.blogz.annotation.OptLog;
import com.zang.blogz.constant.OptTypeConstant;
import com.zang.blogz.enmus.FilePathEnum;
import com.zang.blogz.exception.BizException;
import com.zang.blogz.result.Result;
import com.zang.blogz.strategy.context.UploadStrategyContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: 橘贝贝
 * @Description: 文件上传模块
 * @DateTime: 2022/7/19 15:03
 **/
@RestController
@Api(tags = "文件上传模块")
@RequestMapping("/blogz/fileUpdate")
public class UploadController {

    @Autowired
    private UploadStrategyContext uploadStrategyContext;

    @PostMapping("/admin/uploadArticleCover")
    @ApiOperation(value = "上传文章缩略图",tags = "上传文章缩略图")
    public Result<String> uploadArticleCover(@RequestParam("file") MultipartFile multipartFile) {

        if (multipartFile.isEmpty()) {
            return Result.error("文件有误");
        }

        return Result.ok(uploadStrategyContext.executeUploadStrategy(multipartFile, FilePathEnum.ARTICLE.getPath()));
    }

    @PostMapping("/admin/uploadPageCover")
    @ApiOperation(value = "上传页面封面")
    public Result<String> uploadPageCover(@RequestParam("file") MultipartFile multipartFile) {

        if (multipartFile.isEmpty()) {
            return Result.error("文件有误");
        }

        return Result.ok(uploadStrategyContext.executeUploadStrategy(multipartFile, FilePathEnum.CONFIG.getPath()));
    }

    @PostMapping("/admin/upload/avatar")
    @ApiOperation(value = "上传头像路径")
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile multipartFile){

        if(multipartFile.isEmpty()){
            throw new BizException("文件有误");
        }

        return Result.ok(uploadStrategyContext.executeUploadStrategy(multipartFile,FilePathEnum.AVATAR.getPath()));

    }

    @PostMapping("/admin/upload/photo")
    @ApiOperation(value = "上传相册路径")
    public Result<String> uploadPhoto(@RequestParam("file") MultipartFile multipartFile){

        if(multipartFile.isEmpty()){
            throw new BizException("文件有误");
        }

        return Result.ok(uploadStrategyContext.executeUploadStrategy(multipartFile,FilePathEnum.PHOTO.getPath()));

    }

    @PostMapping("/admin/upload/config")
    @ApiOperation(value = "上传配置图片路径")
    public Result<String> uploadConfig(@RequestParam("file") MultipartFile multipartFile){

        if(multipartFile.isEmpty()){
            throw new BizException("文件有误");
        }

        return Result.ok(uploadStrategyContext.executeUploadStrategy(multipartFile,FilePathEnum.AVATAR.getPath()));

    }




}
